package com.education.basic.action;

import com.education.basic.dao.AdministratorDao;
import com.education.basic.entity.Administrator;
import com.education.basic.resource.Title;
import com.education.basic.service.AdministratorService;
import com.yunshangwoniu.core.baseAction.BaseAction;
import com.yunshangwoniu.core.cache.CacheAble;
import com.yunshangwoniu.core.page.Page;
import com.yunshangwoniu.core.utils.MD5Utils;
import com.yunshangwoniu.core.utils.RequestUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Scope("prototype")
@Namespace("/administrator")
@ParentPackage(value = "struts-default")
public class AdministratorAction extends BaseAction {
    /**
     *
     */
    private static final long serialVersionUID = -5450465108204826982L;
    @Resource
    private AdministratorService administratorService;
    @Resource
    private AdministratorDao administratorDao;
    @Resource
    private CacheAble<Object, Object> cacheAble;

    private List<Title> titles;
    private List<Administrator> administrators;
    private Administrator administrator;
    private Page<Administrator> page;
    private Integer pageNo;
    private String name;
    private long id;
    private String ids;

    @Action(value = "list", results = {@Result(name = "success", location = "list.jsp"), @Result(name = "authority",
            location = "/authority.html")})
    public String list() {
        Administrator ad = (Administrator) cacheAble.get(RequestUtils.getIp(getRequest()));
        if (ad == null || (ad != null && ad.getLevel() != 1)) {
            return "authority";
        }
        pageNo = pageNo == null ? 1 : pageNo;
        page = administratorService.queryAdministro(pageNo, 10, null, getRequest());
        return SUCCESS;
    }

    @Action(value = "add", results = {@Result(name = "success", location = "add.jsp")})
    public String add() {
        return SUCCESS;
    }

    @Action(value = "save", results = {@Result(name = "success", location = "../inc/message.jsp")})
    public String save() {
        Administrator administrator1 = administratorDao.getByUsername(Administrator.class, administrator.getUsername
                ()); //避免用户名重复
        if (administrator1 == null) {
            administrator.setPassword(MD5Utils.KL(MD5Utils.MD5(administrator.getPassword())));
            administratorService.saveObject(administrator);
        }
        setCallback("/administrator/list.htm");
        return SUCCESS;
    }

    @Action(value = "delete")
    public void delete() throws IOException {
        administratorService.deleteObjectById(Administrator.class, id);
        PrintWriter pw = getResponse().getWriter();
        pw.print("success");
        pw.flush();
        pw.close();
    }

    @Action(value = "batchDelete")
    public void batchDelete() throws IOException {

        String[] arr = ids.split("\\,");
        List<Long> ids2 = new ArrayList<Long>();
        for (String str : arr) {
            ids2.add(Long.parseLong(str));
        }
        administratorService.batchDeleteObject(ids2, Administrator.class);
        PrintWriter pw = getResponse().getWriter();
        pw.print("success");
        pw.flush();
        pw.close();
    }

    @Action(value = "edit", results = {@Result(name = "success", location = "edit.jsp")})
    public String edit() {
        administrator = administratorService.getObjectById(Administrator.class, id);
        return "success";
    }

    @Action(value = "modify", results = {@Result(name = "success", location = "../inc/message.jsp")})
    public String modify() {
        administratorService.updateObject(administrator);
        setCallback("/administrator/list.htm?pageNo" + pageNo);
        return SUCCESS;
    }

    @Action(value = "search", results = {@Result(name = "success", location = "list.jsp")})
    public String search() {
        pageNo = pageNo == null ? 1 : pageNo;
        Map<String, Object> params = new HashMap<String, Object>();
        if (!"".equals(name)) {
            params.put("name", name);
        }
        page = administratorService.queryAdministro(pageNo, 10, params, getRequest());
        return SUCCESS;
    }

    public AdministratorService getAdministratorService() {
        return administratorService;
    }

    public void setAdministratorService(AdministratorService administratorService) {
        this.administratorService = administratorService;
    }

    public List<Title> getTitles() {
        return titles;
    }

    public void setTitles(List<Title> titles) {
        this.titles = titles;
    }

    public List<Administrator> getAdministrators() {
        return administrators;
    }

    public void setAdministrators(List<Administrator> administrators) {
        this.administrators = administrators;
    }

    public Administrator getAdministrator() {
        return administrator;
    }

    public void setAdministrator(Administrator administrator) {
        this.administrator = administrator;
    }

    public Page<Administrator> getPage() {
        return page;
    }

    public void setPage(Page<Administrator> page) {
        this.page = page;
    }

    public Integer getPageNo() {
        return pageNo;
    }

    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getIds() {
        return ids;
    }

    public void setIds(String ids) {
        this.ids = ids;
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public AdministratorDao getAdministratorDao() {
        return administratorDao;
    }

    public void setAdministratorDao(AdministratorDao administratorDao) {
        this.administratorDao = administratorDao;
    }

    public CacheAble<Object, Object> getCacheAble() {
        return cacheAble;
    }

    public void setCacheAble(CacheAble<Object, Object> cacheAble) {
        this.cacheAble = cacheAble;
    }

}
